/**
 * @(#) CertificateMediator.as
 *
 * @date		2009. 02. 17.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	제증명신청 - Mediator
 * 				제증명 발행을 조회하고 신청하는 Presentation Logic을 관리 
 */
package com.skt.common_hr.certificate.view
{
	import com.skt.common_hr.certificate.CertificateFacade;
	import com.skt.common_hr.certificate.events.NoticeEvent;
	import com.skt.common_hr.certificate.model.CertificateProxy;
	import com.skt.common_hr.certificate.model.vo.CertificateSaveVO;
	import com.skt.common_hr.certificate.model.vo.CertificateVO;
	import com.skt.common_hr.certificate.view.components.CertificateView;
	import com.skt.common_hr.certificate.view.components.ReqCerti;
	import com.skt.common_hr.certificate.view.components.popRegnoChk;
	import com.skt.common_hr.common.PsHrAlert;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import mx.core.IFlexDisplayObject;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class CertificateMediator extends OverViewMediator
	{
		public static const NAME:String = "CertificateMediator"; 


		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function CertificateMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		override protected function activatedView():void
 		{
 			loadCertiList();
 			setEventListeners();
 		}
 		
 		override protected function closedView():void
 		{
			removeEventListeners(); 			
 		}
 		
 		private function setEventListeners():void
 		{
 			view.addEventListener(CertificateView.InsertCertificate, onNewCerti);
 			view.addEventListener(CertificateView.UpdateCertificate, onUpdateCerti);
 			view.addEventListener(CertificateView.DeleteCertificate, onDelCerti);
 			view.addEventListener(CertificateView.Serach, loadCertiListCall);
 			view.addEventListener(NoticeEvent.SELECTED_ITEM, onDetailView);
 		}
 		
 		private function removeEventListeners():void
 		{
 			view.removeEventListener(CertificateView.InsertCertificate, onNewCerti);
 			view.removeEventListener(CertificateView.UpdateCertificate, onUpdateCerti);
 			view.removeEventListener(CertificateView.DeleteCertificate, onDelCerti);
 			view.removeEventListener(CertificateView.Serach, loadCertiListCall);
 			view.removeEventListener(NoticeEvent.SELECTED_ITEM, onDetailView);
 		}
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return basicInterestMessages.concat([
					CertificateFacade.LOAD_CERTIFICATE_LIST_COMPLETE,
					CertificateFacade.LOAD_CERTIFICATE_LIST_FAILED,
					CertificateFacade.INSERT_CERTIFICATE_COMPLETE,
					CertificateFacade.INSERT_CERTIFICATE_FAILED,
					CertificateFacade.UPDATE_CERTIFICATE_COMPLETE,
					CertificateFacade.UPDATE_CERTIFICATE_FAILED,
					CertificateFacade.DELETE_CERTIFICATE_COMPLETE,
					CertificateFacade.DELETE_CERTIFICATE_FAILED,
					CertificateFacade.PRINT_CERTIFICATE_COMPLETE,
					CertificateFacade.PRINT_CERTIFICATE_FAILED,
					CertificateFacade.PRINT_CERTIFICATENOS_COMPLETE,
					CertificateFacade.PRINT_CERTIFICATENOS_FAILED,
					CertificateFacade.OK_REQUEST_INPUT,
					CertificateFacade.CANCEL_REQUEST_INPUT,
					
					CertificateFacade.PRINT_CERTIFICATE_WONCHUN_COMPLETE,
					]);
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName()) 
			{
				case CertificateFacade.LOAD_CERTIFICATE_LIST_COMPLETE:
					onLoadCertificateList(note.getBody() as CertificateProxy);
					break;
				case CertificateFacade.LOAD_CERTIFICATE_LIST_FAILED:
					break;
				case CertificateFacade.OK_REQUEST_INPUT:
					mx.managers.PopUpManager.removePopUp(view.popup);
					loadCertiList();
				case CertificateFacade.CANCEL_REQUEST_INPUT:
					facade.removeMediator(ReqCertiMediator.NAME);
					mx.managers.PopUpManager.removePopUp(view.popup);
					view.popup = new ReqCerti;
					//view.currentState = "";
					break;
				case CertificateFacade.DELETE_CERTIFICATE_COMPLETE:
					loadCertiList();
					break;
				case CertificateFacade.PRINT_CERTIFICATE_COMPLETE:
					onLoadPrintCertificate(note.getBody() as CertificateProxy);					
					break;

				case CertificateFacade.PRINT_CERTIFICATE_WONCHUN_COMPLETE:
					onLoadPrintCertificateWonchun(note.getBody() as CertificateProxy);					
					break;
					
				case CertificateFacade.PRINT_CERTIFICATE_FAILED:
					PsHrAlert.showMessage((note.getBody() as CertificateProxy).retText);								
					break;															
					
//				case CertificateFacade.PRINT_CERTIFICATE_COMPLETE:
//					onLoadPrintCertificateNos(note.getBody() as CertificateProxy);					
//
//					break;	
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
		
		
		
 		private function onLoadCertificateList(proxy:CertificateProxy):void
 		{
 			view.certiColl = proxy.certiList;
 			//setStatusMessage("데이터가 정삭적으로 로드되었습니다.");
 		}
		
		/* SAC 팝업 콜 */
		private function onLoadPrintCertificate(proxy:CertificateProxy):void
 		{
 			view.clickOutputBtn(proxy.certiPrintList, proxy.certiPrintType);
 		}
 		
 		
 		/* NOS pdf 콜 */
 		private function onLoadPrintCertificateWonchun(proxy:CertificateProxy):void
 		{
 			loadCertiList();
 			PopUpManager.removePopUp(_pop);
 			
			var req:URLRequest = new URLRequest("../certi/"+proxy.certiPrintWonchunUrl);			
			navigateToURL(req); 			
 		} 		
 		
 		/* NOS 팝업 콜 */
 		private function onLoadPrintCertificateNos(vo:CertificateVO):void
 		{
 			view.clickOutputBtnNos(vo);
 		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////

		private function onNewCerti(event:Event):void
		{
			facade.registerMediator(new ReqCertiMediator(view.popup, null));
		}

		private function onUpdateCerti(event:Event):void
		{
			facade.registerMediator(new ReqCertiMediator(view.popup, findCertiRec(view.selSeq)));
		}

		private function onDelCerti(event:Event):void
		{
 			var vo:CertificateSaveVO = new CertificateSaveVO;
 			vo.SEQNR	= view.selSeq;
			
 			var proxy:CertificateProxy = facade.retrieveProxy(CertificateProxy.NAME) as CertificateProxy;
 			proxy.del(Variables.myPernr, vo); 
			
		}

 		private function loadCertiList():void
 		{
 			var proxy:CertificateProxy = facade.retrieveProxy(CertificateProxy.NAME) as CertificateProxy;
 			proxy.load(Variables.myPernr);
 		}
 		
 		private function loadCertiListCall(e:Event):void
 		{
 			var proxy:CertificateProxy = facade.retrieveProxy(CertificateProxy.NAME) as CertificateProxy;
 			proxy.load(Variables.myPernr);
 		}
 		
		private function findCertiRec(seq:String):CertificateVO
		{
			for each(var vo:CertificateVO in view.certiColl) 
			{
				if(vo.SEQNR==seq) {
					return vo;
				}
			}
			trace("not found record: "+ seq);
			return null;
		}
		
		public var _pop:IFlexDisplayObject = null;
		public var oSltdVo:CertificateVO;
		private function onDetailView(event:NoticeEvent):void
		{
			
			if(Variables.company == "SAC"){
				/* SAC */
				var proxy:CertificateProxy = facade.retrieveProxy(CertificateProxy.NAME) as CertificateProxy;
 				proxy.loadPrint(event.selectedItem);	
			}else {
				/* NOS  */
				var sltdVo:CertificateVO = event.selectedItem as CertificateVO;
				oSltdVo = sltdVo;
				// 원천징수 영수증.. sap호출후 pdf 바로 출력에서 민증 체크 후 출력으로 변경 
				if( (sltdVo.DTYPE=='04' || sltdVo.DTYPE=='05' || sltdVo.DTYPE=='06')   && sltdVo.STATE=='2') {

					_pop = PopUpManager.createPopUp(view, popRegnoChk, true);
	  				PopUpManager.centerPopUp(_pop);
	  				popRegnoChk(_pop).fPrintWonchun =  printWonchun;
	  				_pop.y = 150;
				}
				// 재직증명서 .. 팝업후 출력 
				else {
					onLoadPrintCertificateNos(sltdVo);	
				}
			   	
			}
		} 
		
		public function printWonchun(sRegno:String):void {
			var proxy:CertificateProxy = facade.retrieveProxy(CertificateProxy.NAME) as CertificateProxy;
 			proxy.loadPrintWonchun(oSltdVo, sRegno);			
 			
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():CertificateView
        {
            return viewComponent as CertificateView;
        }
	}
}